﻿package ui.components.scroll{
	import flash.display.SimpleButton
	import flash.text.TextFormatAlign
	import flash.events.Event
	import flash.events.MouseEvent
	import flash.display.Sprite;
	import flash.display.Graphics
	import flash.utils.setInterval
	import flash.utils.clearInterval
	import ui.components.SampleButton
	import ui.components.Box
	public class ScrollButton extends SampleButton {
		
		
		function ScrollButton(direction:int = 0, size:int = 10, clickHandler:Function = null){
			//super(clickHandler)
			this.upState 		= new Box().margin(0,0,0,0).fill(0x0,1,3).minSize(size,size).add(new Box().add(drawArrow(0xffffff,size/3,direction)))
			this.downState 		= new Box().margin(0,0,0,0).fill(0xffffff,1,3).minSize(size,size).add(new Box().add(drawArrow(0x888888,size/3,direction)))
			this.overState 		= new Box().margin(0,0,0,0).fill(0xffffff,1,3).minSize(size,size).add(new Box().add(drawArrow(0x0,size/3,direction)))
			this.hitTestState 	= new Box().margin(0,0,0,0).fill(0x0,1,3).minSize(size,size).add(new Box().add(drawArrow(0xffffff,size/3,direction)))
			
			
			if(clickHandler != null){
				this.addEventListener(MouseEvent.MOUSE_DOWN, function(e:MouseEvent):void{
					clickHandler()					
					var n:int = 0;
					var i:Number = setInterval(function():void{
						if(e.target.upState.parent){
							clearInterval(i)
							return;
						}
						if(e.target.downState.parent && n++ > 5)clickHandler()									 
					},50)
					e.target.addEventListener(MouseEvent.MOUSE_UP, function(e:MouseEvent):void{
						clearInterval(i);
						e.target.removeEventListener(MouseEvent.MOUSE_UP, arguments.callee)
					})				
					
				})
			}
		}
		
		private function drawArrow(color:int, size:int, direction:int = 0):Sprite{
			var s:Sprite = new Sprite();
			var g:Graphics = s.graphics;
			var offset:Number = Math.PI/2 * direction
			g.beginFill(color,1);
			g.moveTo(Math.cos(2.0943-offset)*size+size,Math.sin(2.0943-offset)*size+size)
			g.lineTo(Math.cos(4.18879-offset)*size+size,Math.sin(4.18879-offset)*size+size)
			g.lineTo(Math.cos(-offset)*size+size,Math.sin(-offset)*size+size)
			return s
		}
	}
}